package com.ai.module.mj.helper;

import com.ai.module.chat.util.RedisTemplateUtils;
import com.ai.module.chat.util.SpringUtils;
import com.ai.module.mj.domain.Task;
import com.ai.module.mj.service.TaskService;
import com.ai.module.mj.service.HighTaskStoreService;
import com.ai.module.mj.service.LowTaskStoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: MrHe
 * @create: 2024-02-07 16:15
 * @description: 进行初始化后的操作
 **/

@Component
@Slf4j
@Order(Integer.MAX_VALUE)
public class ChatGptRunnerApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("容器初始化结束");
        initEndTask(HighTaskStoreService.HIGH_END_TASK_KEY);
        initEndTask(LowTaskStoreService.SLOW_END_TASK_KEY);
    }
    public void initEndTask(String redisKey) {
        long start = System.currentTimeMillis();
        // 查询所有的任务  进行数据操作
        List<Task> highTask = RedisTemplateUtils.lGet(redisKey,0,-1);
        highTask.forEach(task -> {
            SpringUtils.getClazz (TaskService.class).submitImagine(task);
        });
        RedisTemplateUtils.remove(redisKey);
        log.info("处理停滞任务数[{}],耗时[{}]",highTask.size(), System.currentTimeMillis()-start);
    }

}
